/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 *
 */

import fs from 'fs-extra';
import path from 'path';

import { AbstractProcessHarArtifacts } from '../../abstract/abstract-process-har-artifacts.js';
import { TargetTaskService } from '../../service/target-task-service.js';
import { CacheNativeLibs } from '../../cache-native-libs.js';
import { LegacyHookCompileResource } from '../hook/legacy-hook-compile-resource.js';
import { ArktSTaskConst, CommonConst } from '../../../const/common-const.js';
import { OhosLogger } from '../../../utils/log/ohos-logger.js';
import { TaskNames } from '../../common/task-names.js';
import LegacyFATask = TaskNames.LegacyFATask;

/**
 * 收集FA模型打包需要用的构建产物
 */
export class LegacyProcessHarArtifacts extends AbstractProcessHarArtifacts {
  protected logger = OhosLogger.getLogger(LegacyProcessHarArtifacts.name);
  constructor(taskService: TargetTaskService) {
    super(taskService, LegacyFATask.PROCESS_HAR_ARTIFACTS);
  }

  initTaskDepends() {
    this.declareDepends(CacheNativeLibs.name);
    this.declareDepends(LegacyHookCompileResource.name);
    this.declareDepends(ArktSTaskConst.LINT_ARKTS);
  }

  copyCompiledSourceFileToTempDir(): void {
    throw new Error('Har does not support compiled source and package,which api type is FA');
  }

  copyDeclgenToTempDir(): void {
    throw new Error('Har does not support generate declgen and package,which api type is FA');
  }

  copyOtherGenerateFiles(): void {
    if (this._harExtendInfo.isObfuscatedHar()) {
      throw new Error('Har does not support package generate files which api type is FA');
    } else {
      try {
        fs.copySync(
          path.resolve(this.generateDir, this.isOhpmProject ? CommonConst.OH_PACKAGE_JSON5 : CommonConst.PACKAGE_JSON),
          path.resolve(this.taskTmpDir, this.isOhpmProject ? CommonConst.OH_PACKAGE_JSON5 : CommonConst.PACKAGE_JSON),
        );
      } catch (e: any) {
        this.logger.warn('Failed to copy the temporary path when packing the HAR: ', e.message);
      }
    }
  }

  processPackageJson() {
    // empty
  }

  processSourceMaps() {
    // empty
  }
}
